package com.icebergteam.warfacewidget.videos.ui;

import java.util.Date;
import java.util.Locale;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;
import com.icebergteam.warfacewidget.AppManager;
import com.icebergteam.warfacewidget.WidgetProvider;
import com.icebergteam.warfacewidget.core.LogManager;
import com.icebergteam.warfacewidget.core.OnTaskCompleted;
import com.icebergteam.warfacewidget.core.Parser;
import com.icebergteam.warfacewidget.core.RequestTask;
import com.icebergteam.warfacewidget.core.utils.TimeDateUtils;
import com.icebergteam.warfacewidget.videos.data.VideosParser;
import com.icebergteam.warfacewidget.R;

	public class VideosRequestTask extends RequestTask {

		public static final String className = VideosRequestTask.class.getCanonicalName();
		
		private VideosParser videosParser;
		

		public VideosRequestTask(OnTaskCompleted onTaskCompleted, Context context, Intent intent) {
			super(onTaskCompleted, context, intent);
			
			 this.videosParser = new VideosParser(this.intent.getExtras().getBoolean(Parser.KEY_INTENT_RESTORE));
		}

		
		@Override
		protected Integer doInBackground(Void... paramas) {
			try {
				this.videosParser.saveData("http://wf.mail.ru/download_media/video");
			} catch (Exception e) {
				
				LogManager.LogE(VideosRequestTask.className, LogManager.getExecuteMethodName(), e.getLocalizedMessage());
				e.printStackTrace();
				return this.videosParser.getStateRequest();
					
			}
			return this.videosParser.getStateRequest();
		}

		@Override
		protected void onPostExecute(Integer result) {
			this.intent.putExtra(WidgetProvider.TYPE_LIST, WidgetProvider.VIDEOS_TYPE_LIST);
			if(!videosParser.isRestoreKey()){
				this.intent.putExtra(Parser.KEY_INTENT_RESTORE, false);
			}
			WidgetProvider.setTypeCurrentService(WidgetProvider.VIDEOS_TYPE_LIST);
			
			super.onPostExecute(result);
		}
		
		public static void setWidgetState(RemoteViews view, int result){
							
				view.setEmptyView(R.id.videos_list_widget_layout_videos_listview, R.id.videos_list_widget_layout_empty_textview);
				view.setTextViewText(R.id.videos_list_widget_layout_empty_textview,  (result == (Parser.REQUEST_FAILED ) || result == Parser.DB_INVALID) ? AppManager.getContext().getResources().getText(R.string.errorTextView ): AppManager.getContext().getResources().getText(R.string.videos_list_widget_empty_textview) );
				
				try {
					Date lastUpdateDate = AppManager.getDatabase().getVideosTable().getLastUpdate();
					String date = String.format(
							
							"%s", ((lastUpdateDate.equals(new Date(0))) ? "": TimeDateUtils.getDateByLocale(lastUpdateDate, new Locale(AppManager.getLocaleManager().getCurrentUILangId()), "dd.MM.yy" /*HH:mm*//*"dd MMM yyyy HH:mm:ss"*/)));
			
					view.setTextViewText(R.id.widget_control_panel_date_TextView, date);
					
					LogManager.LogV(VideosRequestTask.className, LogManager.getExecuteMethodName(), result == Parser.REQUEST_COMPLETED,  result == (Parser.REQUEST_FAILED ), view);
				} catch (Exception e) {
					LogManager.LogE(VideosRequestTask.className, LogManager.getExecuteMethodName(), e.getLocalizedMessage());
					e.printStackTrace();
				}
			}
	}